from xml.dom import minidom
from 查找没用的图片 import listDir


def updateXibFile(file,tag,classname):
    dom = minidom.parse(file)   #打开xml文档
    root = dom.documentElement    #得到文档元素对象
    list = [x for x in root.getElementsByTagName(tag) if len(x.getAttribute("customClass")) == 0]
    if len(list) > 0:
        for x in list:
            x.setAttribute("customClass",classname)
            x.setAttribute("customModule","zhengwutong")
            x.setAttribute("customModuleProvider","target")
        doc = minidom.Document()
        doc.appendChild(root)
        with open(file, 'w+',encoding='utf-8') as f:
            doc.writexml(f,encoding="UTF-8")
            return True
    return False

def cleanXibFile(file,tag,keyPath):
    dom = minidom.parse(file)   #打开xml文档
    root = dom.documentElement    #得到文档元素对象
    list = [x for x in root.getElementsByTagName(tag) if x.getAttribute("keyPath") == keyPath]
    if len(list) > 0:
        for x in list:
            x.parentNode.removeChild(x)
        doc = minidom.Document()
        doc.appendChild(root)
        with open(file, 'w+',encoding='utf-8') as f:
            doc.writexml(f,encoding="UTF-8")
            return True
    return False

def updateParentClass(file,parentClass,newClass):
    with open(file, 'r',encoding='utf-8') as f:
        oldc = f.read()
        if oldc.find(f": {parentClass} {{") != -1 and oldc.find(f"{newClass}: {parentClass} {{") == -1 :
            c = oldc.replace(f": {parentClass} {{",f": {newClass} {{")
            with open(file, 'w+',encoding='utf-8') as f2:
                f2.write(c)
                return True
    return False
# # updateXibFile('/Users/wangshuguan/Workplace/zhengwutong5/zhengwutong/Modules/FontViewController.xib')
# updateParentClass("/Users/wangshuguan/Workplace/zhengwutong5/zhengwutong/Modules/Applications/Eat/View/EatMoreHeaderView.swift","UIButton","FontButton")
def updateClass(basedir,xibTag,xibClass,sourceClass,sourceNewClass):
    imageURLs = listDir(basedir, ["xib"], reuslts=[])
    count = 0
    for f in imageURLs:
        if updateXibFile(f,xibTag,xibClass):
            count = count+1

    imageURLs2 = listDir(basedir, ["swift"], reuslts=[])
    for f in imageURLs2:
        if updateParentClass(f,sourceClass,sourceNewClass):
            count = count+1

    print(f"更新{count}个文件")

def cleanXibTag(basedir,xibTag,keyPath):
    imageURLs = listDir(basedir, ["xib"], reuslts=[])
    count = 0
    for f in imageURLs:
        if cleanXibFile(f,xibTag,keyPath):
            count = count+1
    print(f"更新{count}个文件")
basedir = "/Users/wangshuguan/Workplace/zhengwutong5/zhengwutong"
# cleanXibTag(basedir,"userDefinedRuntimeAttribute","fontSize")
# cleanXibTag(basedir,"userDefinedRuntimeAttribute","largeFont")
# cleanXibTag(basedir,"userDefinedRuntimeAttribute","middleFont")
updateClass(basedir,"button","ZWTButton","UIButton","ZWTButton")
updateClass(basedir,"textView","ZWTTextView","UITextView","ZWTTextView")
updateClass(basedir,"textField","ZWTTextField","UITextField","ZWTTextField")
updateClass(basedir,"label","ZWTLabel","UILabel","ZWTLabel")
updateClass(basedir,"imageView","ZWTImageView","UIImageView","ZWTImageView")

